package audio;

import java.net.URL;

import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;
import com.jmex.audio.MusicTrackQueue.RepeatType;

public class Audio {
	private AudioSystem audioSystem;
	private AudioTrack track;
	private AudioTrack music;
	private boolean loaded;
	private RepeatType 	repeatMode;
	private float		fadeIn;
	private float		fadeOut;
	private boolean		play;
	
	public Audio() {
		//prende un handle al sistema audio
		audioSystem = AudioSystem.getSystem();
	}
	
	private AudioTrack getMusic(URL resource) {
		// Create a non-streaming, non-looping, relative sound clip.
		AudioTrack sound = AudioSystem.getSystem().createAudioTrack(resource, true);
		sound.setType(TrackType.MUSIC);
		sound.setRelative(true);
		sound.setTargetVolume(0.7f);
		sound.setLooping(false);
		return sound;
	}

	private AudioTrack getSFX(URL resource) {
		
		System.out.println("AUDIO: path="+resource.getPath());
		
		// Create a non-streaming, looping, positional sound clip.
		AudioTrack sound = AudioSystem.getSystem().createAudioTrack(resource, false);
		sound.setType(TrackType.POSITIONAL);
		sound.setRelative(false);
		sound.setLooping(false);
		return sound;
	}
	
	/**
	 * @author Massimiliano Palermo
	 * @param audioPath : Percorso del file
	 * @param repeat    : modo di esecuzione
	 * @param fadeIn    : fade ingresso
	 * @param fadeOut   : fade uscita
	 * @param play      : se vero, inizia a suonare.
	 */
	protected void loadMusic(String audioPath, 
			                  boolean looping, 
			                  float fadeIn, 
			                  float fadeOut, 
			                  boolean play) throws Exception{
		if(loaded) return;
		
		URL path = Audio.class.getClassLoader().getResource(audioPath);
		System.out.println("TRACK PATH: " + path);
		this.play=play;
		this.fadeIn=fadeIn;
		this.fadeOut=fadeOut;
		track = AudioSystem.getSystem().createAudioTrack(path, false);
		track.setEnabled(true);
		track.setLooping(looping);
		track.setResource(path);
		track.setMaxAudibleDistance(00.0f);
		track.setReferenceDistance(10.0f);
		track.setType(TrackType.MUSIC);
		track.setVolume(5f);
		if (play) track.play();
		loaded=true;
	}
	
	protected void loadFX(String audioPath, boolean looping, float fadeIn, float fadeOut, boolean play) {
		if(loaded) return;
		
		URL path = Audio.class.getClassLoader().getResource(audioPath);
		System.out.println("TRACK PATH: " + path);
		this.play=play;
		this.fadeIn=fadeIn;
		this.fadeOut=fadeOut;
		track = AudioSystem.getSystem().createAudioTrack(path, false);
		track.setEnabled(true);
		track.setLooping(looping);
		track.setResource(path);
		track.setMaxAudibleDistance(10.0f);
		track.setReferenceDistance(0.0f);
		track.setType(TrackType.POSITIONAL);
		track.setVolume(0.5f);
		if (play) track.play();
		loaded=true;
	}
	
	public void Play()
	{
		track.play();
	}
	
	public void Stop()
	{
		track.stop();
	}
	
	public void Pause()
	{
		track.pause();
	}
	
	public boolean isPlaying()
	{
		return track.isPlaying();
	}
	
	public void setVolume(float volume)
	{
		track.setVolume(volume);
		track.setTargetVolume(volume);
	}
	
	public void setPosition(float x, float y, float z)
	{
		track.setWorldPosition(x, y, z);
	}
}

